package com.click369.mianshiqingbao.service.impl;

import com.click369.mianshiqingbao.mapper.ReadLogBeanMapper;
import com.click369.mianshiqingbao.pojo.ReadLogBean;
import com.click369.mianshiqingbao.pojo.ReadLogBeanExample;
import com.click369.mianshiqingbao.pojo.ReadLogBeanExample.Criteria;
import com.click369.mianshiqingbao.pojo.entity.PageResult;
import com.click369.mianshiqingbao.service.ReadLogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 用户阅读记录信息的业务访问接口实现类
 */
@Service
public class ReadLogServiceImpl implements ReadLogService{
    @Autowired
    private ReadLogBeanMapper readLogBeanMapper;
    /**
     * 条件分页查询用户阅读记录信息列表
     * @return 分页查询信息的封装类对象
     */
    @Override
    public PageResult findPage(ReadLogBean readlogbean, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<ReadLogBean> page=(Page<ReadLogBean>)readLogBeanMapper.selectByExample(null);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 增加用户阅读记录信息
     * @param readlogbean 用户阅读记录信息对象
     */
    @Override
    public void add(ReadLogBean readlogbean) {
        readLogBeanMapper.insert(readlogbean);
    }
    /**
     * 根据ID获取用户阅读记录信息对象
     * @param id 用户阅读记录信息的id
     * @return 用户阅读记录信息对象
     */
    @Override
    public ReadLogBean findOne(String id) {
        return readLogBeanMapper.selectByPrimaryKey(id);
    }
    /**
     * 修改用户阅读记录信息
     * @param readlogbean 用户阅读记录信息对象
     */
    @Override
    public void update(ReadLogBean readlogbean) {
        readLogBeanMapper.updateByPrimaryKey(readlogbean);
    }
    /**
     * 批量删除用户阅读记录信息
     * @param ids 批量删除用户阅读记录信息的id集合
     */
    @Override
    public void delete(String[] ids) {
        for(String id:ids){
            readLogBeanMapper.deleteByPrimaryKey(id);
        }
    }
    /**
     * 获取当前用户所选择的一级分类对应的阅读记录列表
     * @param userId 当前登陆的用户ID
     * @param classifyId 当前用户所选择的一级分类ID
     * @return 最近阅读记录列表
     */
    @Override
    public List<ReadLogBean> getRecentReadLog(Long userId, Integer classifyId) {
        ReadLogBeanExample example = new ReadLogBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicClassifyOneEqualTo(classifyId);
        example.setOrderByClause("read_time");
        List<ReadLogBean> readloglist = readLogBeanMapper.selectByExample(example);
        Collections.reverse(readloglist);
        if(readloglist.size()>10){
            return readloglist.subList(0,10);
        }
        return readloglist;
    }
    /**
     * 获取当前用户ID和题目ID对应的阅读记录
     * @param userId 当前登陆的用户ID
     * @param topicId 题目Id
     * @return 阅读记录对象
     */
    @Override
    public ReadLogBean getReadLogBean(Long userId, Long topicId) {
        ReadLogBeanExample example = new ReadLogBeanExample();
        Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicIdEqualTo(topicId);
        List<ReadLogBean> readLogBeanList = readLogBeanMapper.selectByExample(example);
        if(readLogBeanList.size()>0){
            return readLogBeanList.get(0);
        }
        return null;
    }

}
